<?php

class PurposeCateAction extends CommonAction{
public function index()
{
$condition['is_delete'] = 0;
$this->assign('default_map',$condition);
$map = $this->_search ();
if($this->get('default_map'))
$map = array_merge($map,$this->get('default_map'));
if (method_exists ( $this,'_filter')) {
$this->_filter ( $map );
}
$name=$this->getActionName();
$model = D ($name);
if (!empty ( $model )) {
$this->_list ( $model,$map );
}
$this->display ();
return;
}
public function trash()
{
$condition['is_delete'] = 1;
$this->assign('default_map',$condition);
parent::index();
}
public function add()
{
$city_list = M('DealCity')->where('is_delete = 0 and is_effect = 1')->findAll();
$this->assign('city_list',$city_list);
$this->assign('newsort',M(MODULE_NAME)->max('sort')+1);
$this->display();
}
public function insert() {
B('FilterString');
$data = M(MODULE_NAME)->create ();
$this->assign('jumpUrl',u(MODULE_NAME.'/add'));
if(!check_empty($data['name']))
{
$this->error(L('DEALCATE_NAME_EMPTY_TIP'));
}
$log_info = $data['name'];
$list=M(MODULE_NAME)->add($data);
if (false !== $list) {
foreach($_REQUEST['city_id'] as $kk=>$vv){
if(intval($vv) >0){
$link_data['cate_id'] = $list;
$link_data['city_id'] = $vv;
$link_data['recommend'] = intval($_REQUEST['recommend'][$kk]);
$link_data['rec_sort'] = intval($_REQUEST['rec_sort'][$kk]);
M('PurposeCateCityLink')->add($link_data);
}
}
clear_auto_cache('store_filter_nav_cache');
$this->success(L('INSERT_SUCCESS'));
}else {
save_log($log_info.L('INSERT_FAILED'),0);
$this->error(L('INSERT_FAILED'));
}
}
public function edit() {
$id = intval($_REQUEST ['id']);
$condition['id'] = $id;
$vo = M(MODULE_NAME)->where($condition)->find();
$this->assign ( 'vo',$vo );
$city_list = M('DealCity')->where('is_delete = 0 and is_effect = 1')->findAll();
foreach($city_list as $k=>$v){
$city_list[$k]['info'] = M('PurposeCateCityLink')->where('cate_id = '.$id.' AND city_id='.$v['id'])->find();
}
$this->assign('city_list',$city_list);
$this->display ();
}
public function set_sort()
{
$id = intval($_REQUEST['id']);
$sort = intval($_REQUEST['sort']);
$log_info = M(MODULE_NAME)->where('id='.$id)->getField('name');
if(!check_sort($sort))
{
$this->error(l('SORT_FAILED'),1);
}
M(MODULE_NAME)->where('id='.$id)->setField('sort',$sort);
save_log($log_info.l('SORT_SUCCESS'),1);
clear_auto_cache('store_filter_nav_cache');
$this->success(l('SORT_SUCCESS'),1);
}
public function update() {
B('FilterString');
$data = M(MODULE_NAME)->create ();
$log_info = M(MODULE_NAME)->where('id='.intval($data['id']))->getField('title');
$this->assign('jumpUrl',u(MODULE_NAME.'/edit',array('id'=>$data['id'])));
$list=M(MODULE_NAME)->save ($data);
if (false !== $list) {
D('PurposeCateCityLink')->where('cate_id='.$data['id'])->delete();
foreach($_REQUEST['city_id'] as $kk=>$vv){
if(intval($vv) >0){
$link_data['cate_id'] = $data['id'];
$link_data['city_id'] = $vv;
$link_data['recommend'] = intval($_REQUEST['recommend'][$kk]);
$link_data['rec_sort'] = intval($_REQUEST['rec_sort'][$kk]);
M('PurposeCateCityLink')->add($link_data);
}
}
save_log($log_info.L('UPDATE_SUCCESS'),1);
clear_auto_cache('store_filter_nav_cache');
$this->success(L('UPDATE_SUCCESS'));
}else {
save_log($log_info.L('UPDATE_FAILED'),0);
$this->error(L('UPDATE_FAILED'),0,$log_info.L('UPDATE_FAILED'));
}
}
public function delete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
if(M('PurposeCateType')->where(array ('cate_id'=>array ('in',explode ( ',',$id ) )))->count()>0)
{
$this->error (l('SUB_DEALCATE_EXIST'),$ajax);
}
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->setField ( 'is_delete',1 );
if ($list!==false) {
save_log($info.l('DELETE_SUCCESS'),1);
clear_auto_cache('store_filter_nav_cache');
$this->success (l('DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('DELETE_FAILED'),0);
$this->error (l('DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function restore() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->setField ( 'is_delete',0 );
if ($list!==false) {
save_log($info.l('RESTORE_SUCCESS'),1);
clear_auto_cache('store_filter_nav_cache');
$this->success (l('RESTORE_SUCCESS'),$ajax);
}else {
save_log($info.l('RESTORE_FAILED'),0);
$this->error (l('RESTORE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
public function foreverdelete() {
$ajax = intval($_REQUEST['ajax']);
$id = $_REQUEST ['id'];
if (isset ( $id )) {
if(M('PurposeCateType')->where(array ('cate_id'=>array ('in',explode ( ',',$id ) )))->count()>0)
{
$this->error (l('SUB_DEALCATE_EXIST'),$ajax);
}
$condition = array ('id'=>array ('in',explode ( ',',$id ) ) );
$rel_data = M(MODULE_NAME)->where($condition)->findAll();
foreach($rel_data as $data)
{
$info[] = $data['name'];
}
if($info) $info = implode(',',$info);
$list = M(MODULE_NAME)->where ( $condition )->delete();
if ($list!==false) {
save_log($info.l('FOREVER_DELETE_SUCCESS'),1);
clear_auto_cache('store_filter_nav_cache');
$this->success (l('FOREVER_DELETE_SUCCESS'),$ajax);
}else {
save_log($info.l('FOREVER_DELETE_FAILED'),0);
$this->error (l('FOREVER_DELETE_FAILED'),$ajax);
}
}else {
$this->error (l('INVALID_OPERATION'),$ajax);
}
}
}
?>